package com.sf.iisp.bind.dao.impl;

import java.math.BigInteger;
import java.util.List;
import java.util.Set;

import org.hibernate.SQLQuery;

import com.sf.iisp.bind.dao.IVirtualNoDao;
import com.sf.iisp.bind.domain.VirtualNo;
import com.sf.novatar.base.dao.BaseBeanDao;

import org.springframework.stereotype.Repository;

@Repository
public class VirtualNoDao extends BaseBeanDao<Long, VirtualNo> implements IVirtualNoDao {

    @Override
    public VirtualNo saveVirtualNo(VirtualNo virtualNo) {
        saveOrUpdate(virtualNo);
        return virtualNo;
    }

    /**
     * 更新虚拟号码表
     *
     * @param virtualNo
     * @return
     */
    @Override
    public VirtualNo updateVirtualNo(VirtualNo virtualNo) {
        merge(virtualNo);
        return virtualNo;
    }

    /**
     * 根据ids集合删除多条虚拟账号
     *
     * @param ids
     */
    @Override
    public void removeByIds(Set<Long> ids) {
        removeByIds(ids);
    }

    /**
     * 根据id集合删除多条虚拟账号
     *
     * @param id
     */
    @Override
    public void removeById(Long id) {
        remove(load(id));
    }


    @Override
    public VirtualNo getVirtualNo(String virtualNo) {
        String sql = "SELECT t.* from cmdp_customer_c t where t.customer_serial_number =:virtualNo";
        SQLQuery query = this.getCurrentSession().createSQLQuery(sql.toString());
        query.setString("virtualNo", virtualNo);
        query.addEntity(VirtualNo.class);
        VirtualNo vn = (VirtualNo) query.uniqueResult();
        return vn;
    }


    @Override
    public int getBindSum() {
        String sql = " select count(*) from virtual_no";
        SQLQuery query = this.getCurrentSession().createSQLQuery(sql);
        //TODO:这里返回的是BigInteger，不是int或者Long
        BigInteger uniqueResult = (BigInteger) query.uniqueResult();
        return uniqueResult.intValue();
    }

    @Override
    public List<VirtualNo> getVirtualNoList() {
        String sql = " select * from virtual_no";
        SQLQuery query = this.getCurrentSession().createSQLQuery(sql);
        query.addEntity(VirtualNo.class);
        return query.list();
    }


}
